package com.huidao.service;

import java.util.List;

import com.alibaba.fastjson.JSONArray;
import com.huidao.dao.GoodsDao;
import com.huidao.dao.GoodsDaoImp;
import com.huidao.entity.Goods;

public class GoodsServiceImp implements GoodsService {
	private static int pagesize = 5;
	private static GoodsDao gd = new GoodsDaoImp();

	@Override
	public String getGoodsList(String keyName, int currentpage, long id) {
		List<Goods> list = gd.getListGoods(keyName, currentpage, pagesize, id);
		JSONArray array = (JSONArray) JSONArray.toJSON(list);
		 int totalPage = getTotalPage(keyName,id);
		 array.add(totalPage);
		 array.add(currentpage);
		return array.toJSONString();
	}

	@Override
	public int getTotalPage(String keyName, long id) {
		int totalPage = gd.total(keyName,  id);
		if(totalPage%pagesize==0){
			return totalPage/pagesize;
		}else{
			return totalPage/pagesize+1;
		}
	}

	@Override
	public String updateStatusGoods(Goods goods) {
		if(gd.updateGoodsStatus(goods)){
			return "";
		}
		return "修改失败";
	}

	@Override
	public String delGoods(long gid) {
		if(gd.delGoods(gid)){
			return "";
		}else{
			return "删除失败";
		}
	}

	@Override
	public String updateGoods(Goods goods) {
		if(gd.updateGoods(goods)){
			return "";
		}else{
			return "修改失败";
		}
	}

	@Override
	public String addGoods(Goods goods) {
		if(gd.isExitGoods(goods)){
			return "该商品已经存在";
		}
		if(gd.addGoods(goods)){
			return "";
		}else{
			return "添加失败";
		}
	}
}
